#include "mysql.h"
#include "ui_mysql.h"
#include"firstwidget.h"
#include<QDebug>
#include<QSqlError>
#include<QMessageBox>
#include <QFile>
#include <QDir>


mysql::mysql(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::mysql)
{
    ui->setupUi(this);

    ui->edit_hostname->setInputMask("000.000.000.000");

    ui->edit_password->setEchoMode(QLineEdit::Password);
    ui->edit_password->setClearButtonEnabled(true);
    ui->edit_port->setClearButtonEnabled(true);
    ui->edit_user->setClearButtonEnabled(true);
    ui->edit_database->setClearButtonEnabled(true);
    ui->edit_hostname->setClearButtonEnabled(true);
    connect(ui->btn_host_login,&QPushButton::clicked,[=]()
    {
        QString host=ui->edit_hostname->text();
        QString user=ui->edit_user->text();
        QString password=ui->edit_password->text();
        QString database=ui->edit_database->text();
        int port=3306;
        if((ui->edit_port->text().toInt())!=3306)
        {
            port=ui->edit_port->text().toInt();
        }

        if(host.isEmpty()||user.isEmpty()||password.isEmpty()||database.isEmpty())
        {
            QMessageBox::warning(this,"warning","必填项未填!");
        }
        else
        {

//            QString desktopPath = QDir::homePath() + "/Desktop";
              QFile file("./data.txt");

            if (file.open(QIODevice::WriteOnly | QIODevice::Text))
            {
                QTextStream out(&file);
                // 创建一个 QTextStream 对象，关联到打开的文件

                out <<host << "\t" <<user << "\t" << password<<"\t"<<database<<"\t"<<port<<"\n";
                // 将三个 QLineEdit 的数据写入文件，用制表符分隔，最后换行

                file.close();
                // 关闭文件
            }
//            QString desktopPath2 = QDir::homePath() + "/Desktop/data.txt";
            QFile file2("./data.txt");
            if (file2.open(QIODevice::ReadOnly | QIODevice::Text)) {
                QTextStream in(&file2);
                QString line = in.readLine();
                QStringList data = line.split("\t");
                if (data.size() >= 2)
                {
                   host=data[0];
                   user=data[1];
                   password=data[2];
                   database=data[3];
                   port=data[4].toInt();
                }
                file2.close();
            }

            QMessageBox::information(this, "info", "配置成功");
                 mysql::Returnface();

//            db=QSqlDatabase::addDatabase("QMYSQL");
//            db.setHostName(host);
//            db.setUserName(user);
//            db.setPassword(password);
//            db.setDatabaseName(database);
//            db.setPort(port);
//            if(!db.open())
//            {
//                qDebug()<<db.lastError()<<endl;
//                return;
//            }
//            else
//            {
//              qDebug()<<"打开成功"<<endl;
//            }
        }

    });
    connect(ui->btn_host_return,&QPushButton::clicked,this,&mysql::Returnface);
}

mysql::~mysql()
{
    delete ui;
}

void mysql::Returnface()
{
    firstwidget* w = new firstwidget;
    w->show();
    this->hide();
}

